package com.threeox.commonlibrary.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import com.threeox.utillibrary.util.ActivityHelper;
import com.threeox.utillibrary.util.EmptyUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @ClassName: BroadCastUtils
 *
 * @Description: TODO()
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/20 13:53
 *
 * @version 1.0
 *
 */
public class BroadCastUtils {

	private Context mContext;
    private Intent mIntent = null;
    private Class<? extends Intent> mIntentClass;
	private OnBroadcastReceiver onBroadcastReceiver;
    private LocalBroadcastManager mLocalBroadcastManager;
    private List<BroadcastReceiver> mReceivers = new ArrayList<BroadcastReceiver>();

	public static BroadCastUtils getInstance(Context context) {
		return new BroadCastUtils(context);
	}

    private BroadCastUtils(){}

    private BroadCastUtils(Context context) {
        this.mContext = context;
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);
    }

    /**
     * 注册多个广播
     *
     * @param actions
     * @return
     */
    public BroadCastUtils register(int... actions) {
    	if (EmptyUtils.isNotEmpty(actions)) {
			for (int action: actions) {
                register(mContext.getString(action));
			}
		}
		return this;
	}


	/**
	 * 注册多个广播
	 * 
	 * @param actions
	 * @return
	 */
	public BroadCastUtils register(String... actions) {
        if (EmptyUtils.isNotEmpty(actions)) {
            for (String action : actions) {
                register(action);
            }
        }
		return this;
	}
	
	/**
	 * 注册广播
	 * 
	 * @param action
	 */
	public BroadCastUtils register(String action) {
		// 注册广播接收
		BaseBroadCastReceiver receiver = new BaseBroadCastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(action); // 只有持有相同的action的接受者才能接收此广播
        mLocalBroadcastManager.registerReceiver(receiver, filter);
		mReceivers.add(receiver);
		return this;
	}

	/**
	 * 解绑广播
	 */
	public BroadCastUtils unregisterReceiver() {
		if (EmptyUtils.isNotEmpty(mReceivers)) {
			for (BroadcastReceiver receiver : mReceivers) {
				if (receiver != null) {
                    mLocalBroadcastManager.unregisterReceiver(receiver);
				}
			}
		}
		return this;
	}

	public BroadCastUtils setOnBroadcastReceiver(OnBroadcastReceiver onBroadcastReceiver) {
		this.onBroadcastReceiver = onBroadcastReceiver;
		return this;
	}

    public class BaseBroadCastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (onBroadcastReceiver != null) {
				onBroadcastReceiver.onReceive(context, intent, intent.getAction());
			}
		}

	}

	public interface OnBroadcastReceiver {

		void onReceive(Context context, Intent intent, String action);

	}

    /**
     * 传入Intent根据Map
     *
     * @param data
     * @return
     */
    public BroadCastUtils putIntent(Map<String, Object> data) {
        if (data != null) {
            Set<String> set = data.keySet();
            for (String key : set) {
                putIntent(key, data.get(key));
            }
        }
        return this;
    }

    /**
     * 传入Intent
     *
     * @param key
     * @param value
     * @return
     */
	public BroadCastUtils putIntent(String key, Object value) {
		if (mIntent == null) {
			this.mIntent = new Intent();
            mIntentClass = mIntent.getClass();
		}
		ActivityHelper.putExtra(mIntentClass, mIntent, key, value);
		return this;
	}

	/**
	 * 发送广播的方法
	 * 
	 * @param action
	 * @return
	 */
	public BroadCastUtils sendBroadCast(String action) {
		if (mIntent == null) {
			this.mIntent = new Intent();
		}
		mIntent.setAction(action);
        mLocalBroadcastManager.sendBroadcast(mIntent);
		this.mIntent = new Intent();
		return this;
	}

}